<?php
/**
 * Created By PhpStorm.
 * User: RenJianHong
 * Date: 2021-10-29 14:32
 */

namespace Modules\Order\Entities;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Schema;
use Modules\Order\Entities\Order;
use Modules\Order\Entities\OrderMessage;
use Modules\Order\Entities\UserCapitalLog;

class OrderWarn extends BaseModel
{
    public $order_id;

    public $order_type; //订单类型 0--商城订单 1--秒杀订单 2--拼团订单 3--预约订单
    public $order;

    //支付完成之后，相关的操作
    public function notify()
    {
        try {
            $this->OrderNotify();
            
            $order = $this->order;
            //后台订单提醒
            OrderMessage::set($order->id, $this->order_type, 0);
        
        } catch (\Exception $e) {

        }
    }

    //商城
    private function OrderNotify()
    {
        /* @var Order $order */
        $order = $this->order = Order::find($this->order_id);
        
        //余额支付记录保存
        if ($order->pay_type == 1) {
            $data['user_id'] = $order->user_id;
            $data['operate'] = 2;//扣除
            $data['type'] = 1;
            $data['amount'] = $order->pay_price;;
            $data['description'] = '商城余额支付，订单号为：'.$order->order_no;
            
            $result = UserCapitalLog::saveLog($data);
            
            if($result){
                return true;
            }else{
                return false;
            }
        }

        return true;
    }
}